package com.spdg.ring.sqlite.service;

import java.util.List;

import android.content.Context;

import cn.wolf.tools.Log;

import com.spdg.ring.sqlite.dao.ArticleListDao;
import com.spdg.ring.sqlite.dao.impl.ArticleListDaoImpl;
import com.spdg.ring.sqlite.model.ArticleList;
/**
 * <p>Title: ArticleListService      </p>
 * <p>Description: 
 * ArticleListService provides all the ArticleList CRUD 
 * </p>
 * <p>@author: Eric.wsd                </p>
 * <p>Copyright: Copyright (c) 2012    </p>
 * <p>Company: HX Co., Ltd.          </p>
 * <p>Create Time: 2012-11-11             </p>
 * <p>Update Time:                     </p>
 * <p>Updater:                         </p>
 * <p>Update Comments:                 </p>
 */
public class ArticleListService {
	private static ArticleListService articleListService;
	private static ArticleListDao articleListDao;

	private ArticleListService(Context context) {
		articleListDao = ArticleListDaoImpl.newInstance(context);
	}

	public static ArticleListService newInstance(Context context) {
		if (articleListService == null) {
			articleListService = new ArticleListService(context);
		}

		return articleListService;
	}

	/**
	 * get all articlelist objects
	 * @return
	 * 		ArticleList list
	 */
	public List<ArticleList> findAll() {
		return articleListDao.find();
	}
	
	/**
	 * get all articlelist objects , according to fileKey;
	 * @param fileKey
	 * 		parent key
	 * @return
	 */
	public List<ArticleList> findByParentKey(String fileKey) {
		return articleListDao.findByParentKey(fileKey);
	}
	
	/**
	 * 通过article的key获取相关对象
	 * @param fileKey
	 * @return
	 */
	public ArticleList findByKey(String fileKey) {
		return articleListDao.findByKey(fileKey);
	}

	/**
	 * save or update ArticleList , according to file field
	 * @param entity
	 * 		ArticleList object
	 */
	public synchronized void saveOrUpdate(ArticleList entity) {
		if (entity == null) {
			Log.e("ArticleList object is null");
			return;
		}

		if (isExist(entity)) {
			articleListDao.update(entity);
		} else {
			articleListDao.insert(entity);
		}

	}

	/**
	 * whether ArticleList is exist
	 * @param entity
	 * 		ArticleList object
	 * @return
	 * 		boolean
	 */
	public synchronized boolean isExist(ArticleList entity) {
		return articleListDao.isExist(entity);
	}

	/**
	 * remove ArticleList , according to ArtivleList file field;
	 * @param entity
	 * 		ArticleList object;
	 */
	public void remove(ArticleList entity) {
		if (entity == null) {
			Log.e("ArticleList object is null");
			return;
		}

		articleListDao.delete(entity);
	}

	/**
	 * remove all ArticleList objects
	 */
	public void removeAll() {
		articleListDao.deleteAll();
	}

}
